package xml.test;

import java.io.FileInputStream;
import org.custommonkey.xmlunit.XMLUnit;
import org.dbunit.DBTestCase;
import org.dbunit.IDatabaseTester;
import org.dbunit.JdbcDatabaseTester;
import org.dbunit.database.DatabaseConfig;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSet;
import org.dbunit.ext.mysql.MySqlDataTypeFactory;
import org.dbunit.operation.CompositeOperation;
import org.dbunit.operation.DatabaseOperation;
import org.restlet.data.MediaType;
import org.restlet.representation.Representation;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import xml.db.DataSourceFactory;
import xml.db.Database;
import xml.resource.AbstractResource;
import static org.junit.Assert.*;

/**
 *
 * @author Lou
 */
public abstract class ResourceTestCase<T extends AbstractResource> extends DBTestCase {

    public static Database database = new Database(DataSourceFactory.newDataSource());

    abstract protected T createResource();

    public T newResourceInstance() {
        T resource = createResource();
        resource.setDatabase(getDatabase());
        return resource;
    }

    public static Database getDatabase() {
        if (database == null) {
            database = new Database(DataSourceFactory.newDataSource());
            System.out.println(database.toString());
        }
        return database;
    }

    public Document getTestableDocument(Representation representation) {
        assertIsXml(representation);

        Document document = null;
        try {
            InputSource inputSource = new InputSource(representation.getReader());

            document = XMLUnit.buildControlDocument(inputSource);
        } catch (Exception ex) {
            fail("Impossible de créer le document xml à partir de la représentation");
        }

        return document;
    }

    public void assertIsXml(Representation representation) {
        assertTrue(representation.getMediaType().equals(MediaType.TEXT_XML));
    }

    protected IDataSet getDataSet() throws Exception {
        return new FlatXmlDataSet(new FileInputStream("data/schema.xml"));
    }

    @Override
    /** Indispensable pour obtenir une connexion */
    protected IDatabaseTester newDatabaseTester() throws Exception {
        return new JdbcDatabaseTester(DataSourceFactory.DRIVER_NAME,
                DataSourceFactory.URL, DataSourceFactory.USER_NAME,
                DataSourceFactory.PASSWORD);
    }
    
    @Override
    protected void setUpDatabaseConfig(DatabaseConfig config) {
        config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new MySqlDataTypeFactory());
    }

    @Override
    /** Operation sur la base de donnees executee avant tout test */
    protected DatabaseOperation getSetUpOperation() throws Exception {
        DatabaseOperation result = new CompositeOperation(DatabaseOperation.TRUNCATE_TABLE,
                DatabaseOperation.INSERT);
        return result;
    }

    @Override
    /** Operation sur la base de donnees executee avant tout test */
    protected DatabaseOperation getTearDownOperation() throws Exception {
        DatabaseOperation result = new CompositeOperation(DatabaseOperation.TRUNCATE_TABLE,
                DatabaseOperation.INSERT);
        return result;
    }

}
